//
//  FlowDetailViewController+Request.m
//  NaRiLSVMT
//
//  Created by jmsre on 14-8-26.
//  Copyright (c) 2014年 helloyouth.org. All rights reserved.
//

#import "FlowDetailViewController+Request.h"
#import "MBProgressController.h"
#import "Network_Operation.h"

extern NSInteger   lastFlowNumber;//前一个处理的流程No.或者当前

@implementation FlowDetailViewController (Request)



-(void)NRLSMTGetThumbnailByFlowNodeId:(NSString *)flowNodeId
{
    [[MBProgressController sharedInstance] showWithText:@"正在努力加载..."];
//    WeakSelf;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSString *getThumbnailStr = [Network_API Network_API_GetThumbnailByFlowNodeId];
        
        AFHTTPClient *httpClient = [Network_Operation httpClientWithUrl:getThumbnailStr];
        NSString    *str = [NSString stringWithFormat:@"[{\"classname\":%@,\"methodname\":%@,\"parameterClasss\":[{\"parametername\":%@,\"parametervalue\":\"%@\",\"valuetype\":%@},{\"parametername\":%@,\"parametervalue\":\"%@\",\"valuetype\":%@}]}]",@"\"iControlSolutionService\"",@"\"getThumbnailByFlowNodeId\"",@"\"userId\"",subjectId,@"\"java.lang.String\"",@"\"flowNodeId\"",flowNodeId,@"\"java.lang.String\""];
        [httpClient postPath:getThumbnailStr
                  parameters:@{@"jsonstr":str,
                               @"callback":@""
                               }
                     success:^(AFHTTPRequestOperation *operation, id responseObject) {
                         if([responseObject isKindOfClass:[NSString class]])
                         {
                             NSString *resStr = [NSString stringWithString:responseObject];
                             if(!([resStr rangeOfString:@"name"].location == NSNotFound))
                             {
                                 NSLog(@"getThumbnailByFlowNodeId Success");
                                 NSLog(@"%@",flowNodeId);
                                 NSMutableArray *arr = [NSMutableArray arrayWithArray:[Common jsontoArr:resStr]];
                                 
                                [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"nodeThumbnailArr"] addObject:@{flowNodeId: arr}];
                             }
                             else
                             {
                                 dispatch_async(dispatch_get_main_queue(), ^{
                                    // self.navigationController.navigationBar.hidden = YES;
                                     [[MBProgressController sharedInstance] hide];
                                     [[MBProgressController sharedInstance] showTipsOnlyText:@"查询节点播放缩略图失败" AndDelay:1.5];
                                 });
                                 NSLog(@"直接返回");
                             }
                         }
                         else {
                             dispatch_async(dispatch_get_main_queue(), ^{
                              //   self.navigationController.navigationBar.hidden = YES;
                                 [[MBProgressController sharedInstance] hide];
                                 [[MBProgressController sharedInstance] showTipsOnlyText:@"查询节点播放缩略图失败"
                                                                                AndDelay:1.5];
                             });
                             NSLog(@"直接返回");
                         }
                     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                         dispatch_async(dispatch_get_main_queue(), ^{
                           //  self.navigationController.navigationBar.hidden = YES;
                             [[MBProgressController sharedInstance] hide];
                             [[MBProgressController sharedInstance] showTipsOnlyText:@"查询节点播放缩略图失败"
                                                                            AndDelay:1.5];
                         });
                     }];
    });
    [[MBProgressController sharedInstance] hide];
}


-(void)NRLSMTFlowNodeAutoPlay:(NSString *)flowNodeId isTurn:(NSString *)turn
{
//    [[MBProgressController sharedInstance] showWithText:@"正在努力加载..."];
    WeakSelf;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSString *flowNodeAutoPlayStr = [Network_API Network_API_FlowNodeAutoPlay];
        
        AFHTTPClient *httpClient = [Network_Operation httpClientWithUrl:flowNodeAutoPlayStr];
        NSString    *str = [NSString stringWithFormat:@"[{\"classname\":%@,\"methodname\":%@,\"parameterClasss\":[{\"parametername\":%@,\"parametervalue\":\"%@\",\"valuetype\":%@},{\"parametername\":%@,\"parametervalue\":\"%@\",\"valuetype\":%@}]}]",@"\"iControlSolutionService\"",@"\"flowNodeAutoPlay\"",@"\"flowNodeId\"",flowNodeId,@"\"java.lang.String\"",@"\"flag\"",turn,@"\"java.lang.String\""];
        [httpClient postPath:flowNodeAutoPlayStr
                  parameters:@{@"jsonstr":str,
                               @"callback":@""
                               }
                     success:^(AFHTTPRequestOperation *operation, id responseObject) {
                         if([responseObject isKindOfClass:[NSString class]])
                         {
                             NSString *resStr = [NSString stringWithString:responseObject];
                             if(!([resStr rangeOfString:@"status"].location == NSNotFound))
                             {
                                 NSLog(@"flowNodeAutoPlay Success");
                             }
                             else
                             {
                                 dispatch_async(dispatch_get_main_queue(), ^{
                                 //    self.navigationController.navigationBar.hidden = YES;
                                     [[MBProgressController sharedInstance] hide];
                                     [[MBProgressController sharedInstance] showTipsOnlyText:@"流程节点切屏失败" AndDelay:1.5];
                                 });
                                 NSLog(@"直接返回");
                             }
                             if([turn isEqualToString:@"true"])
                             {
                                 // 同一个屏幕
                                 if(!self.nowIndex){
                                     self.nowIndex = flowNodeId;
                                 }
                                 if([self.nowIndex isEqualToString:flowNodeId]){
                                     if(self.oldIndex == nil || [self.oldIndex isEqualToString:@""]){
                                        wself.oldIndex = flowNodeId;
                                     }
                                     else{
                                         if(![self.oldIndex isEqualToString:self.nowIndex])
                                         [self NRLSMTFlowNodeAutoPlay:wself.oldIndex isTurn:@"false"];
                                     }
                                 }
                                 else
                                 {
                                     wself.oldIndex = wself.nowIndex;
                                     if(![wself.oldIndex isEqualToString:@""] && wself.oldIndex != nil){
                                         [self NRLSMTFlowNodeAutoPlay:wself.oldIndex isTurn:@"false"];
                                     }
                                 }
                                 wself.nowIndex = flowNodeId;
                             }
//                             if(![wself.oldIndex isEqualToString:@""] && wself.oldIndex != nil && [turn isEqualToString:@"true"])
//                             {
//                                 if(![wself.oldIndex isEqualToString:flowNodeId])
//                                 [self NRLSMTFlowNodeAutoPlay:wself.oldIndex isTurn:@"false"];
//                             }
//                             if([turn isEqualToString:@"true"])
//                             {
//                                 if(![wself.oldIndex isEqualToString:flowNodeId])
//                                 wself.oldIndex = flowNodeId;
//                             }
                         }
                         else {
                             dispatch_async(dispatch_get_main_queue(), ^{
                             //    self.navigationController.navigationBar.hidden = YES;
                                 [[MBProgressController sharedInstance] hide];
                                 [[MBProgressController sharedInstance] showTipsOnlyText:@"流程节点切屏失败"
                                                                                AndDelay:1.5];
                             });
                             NSLog(@"直接返回");
                         }
                     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                         dispatch_async(dispatch_get_main_queue(), ^{
                          //   self.navigationController.navigationBar.hidden = YES;
                             [[MBProgressController sharedInstance] hide];
                             [[MBProgressController sharedInstance] showTipsOnlyText:@"流程节点切屏失败"
                                                                            AndDelay:1.5];
                         });
                     }];
    });
    [[MBProgressController sharedInstance] hide];
}



@end
